package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.SysCompeScoringDetails;
import com.ruoyi.system.domain.mongodb.Users;
import com.ruoyi.system.mapper.SysCompeScoringDetailsMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mongo.SysUserDao;
import com.ruoyi.system.service.CompeWorkScroingRecordService;
import com.ruoyi.system.service.SysCompeScoringDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 柳
 * @Date 2021/3/30
 * @Description TODO
 * 评审员评分详情
 */
@Service
public class SysCompeScoringDetailsServiceImpl implements SysCompeScoringDetailsService {
    @Autowired
    private SysCompeScoringDetailsMapper sysCompeScoringDetailsMapper;

    @Autowired
    private CompeWorkScroingRecordService compeWorkScroingRecordService;

    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysUserMapper sysUserMapper;


    @Override
    public List<SysCompeScoringDetails> querySysCompeScoringDetailsIsAdmin(SysCompeScoringDetails sysCompeScoringDetails) {
        return sysCompeScoringDetailsMapper.querySysCompeScoringDetailsIsAdmin(sysCompeScoringDetails);
    }

    /**
     * 根据赛事查询评分详情信息
     * @param sysCompeScoringDetails
     * @return
     */
    @Override
    public List<SysCompeScoringDetails> querySysCompeScoringDetails(SysCompeScoringDetails sysCompeScoringDetails) {
        boolean isAdmin = false; //是否为管理员
        try {
            Long userId = null;
            userId = Long.parseLong(sysCompeScoringDetails.getUser_id());
            //获取当前登录者的账户
            SysUser sysUser = sysUserMapper.selectUserById(userId);
            if (sysUser!= null ) {
                //获取当前用户的角色
                List<SysRole> roles = sysUser.getRoles();;
                if (roles != null) {
                    for (SysRole role : roles) {
                        //如果当前用户有管理员角色
                        if ("common".equals(role.getRoleKey()) ) {
                            isAdmin = true;
                            break;
                        }
                    }
                }
            }
        } catch (NumberFormatException e) {
            //查询mongodb查看该老师是不是校园管理员
            Users users = sysUserDao.queryUserById(sysCompeScoringDetails.getUser_id());
            if (users != null && users.getIsAdmin()==1) {
                isAdmin = true;
            }
        }
        if (isAdmin) {
            return sysCompeScoringDetailsMapper.querySysCompeScoringDetailsIsAdmin(sysCompeScoringDetails);
        } else {
            return sysCompeScoringDetailsMapper.querySysCompeScoringDetails(sysCompeScoringDetails);
        }
    }



    /**
     * 添加评分详情信息
     * @param sysCompeScoringDetails
     */
    @Transactional
    @Override
    public void saveSysCompeScoringDetails(SysCompeScoringDetails sysCompeScoringDetails) {
        SysCompeScoringDetails item = sysCompeScoringDetailsMapper.queryByScoringId(sysCompeScoringDetails);
        if (item != null) {
            sysCompeScoringDetailsMapper.updateSysCompeScoringDetails(sysCompeScoringDetails);
        } else {
            sysCompeScoringDetailsMapper.saveSysCompeScoringDetails(sysCompeScoringDetails);
        }
    }
}
